<?php
class FilterRequest extends PageElement
{
	// $name is in parent class

	protected $filterType = "";  // Checkbox, Date, SingleEnum, MultiEnum
	protected $field = ""; 
	protected $value = "";
	protected $selectEqual = true;
	protected $selectAfter = true;
	protected $enabled = false;
	protected $invertEnabled = false;
	protected $isORCondition = true;

	public function setFilterType($filterType)
	{
		$this->filterType = $filterType;
	}

	public function setField($field)
	{
		$this->field = $field;
	}

	public function setvalue($value)
	{
		$this->value = $value;
	}

	public function setSelectEqual($selectEqual)
	{
		$this->selectEqual = $selectEqual;
	}

	public function setSelectAfter($selectAfter)
	{
		$this->selectAfter = $selectAfter;
	}

	public function setInvertEnabled($invertEnabled)
	{
		$this->invertEnabled = $invertEnabled;
	}

	public function setEnabled($enabled)
	{
		$this->enabled = $enabled;
	}

	public function setIsORCondition($isORCondition)
	{
		$this->isORCondition = $isORCondition;

	}
	public function getIsORCondition()
	{
		return $this->isORCondition;
	}

	public function getFilterCondition()
	{
		$condition = "";
		switch ($this->filterType)
		{
			case "Checkbox":
				if ($this->selectEqual)
					$operator = " = ";
				else
					$operator = " <> ";

				if ($this->enabled xor $this->invertEnabled)
					$condition = $this->field.$operator."'".$this->value."'";

				break;

			case "Date":
				if ($this->selectAfter)
					$operator = " >= ";
				else
					$operator = " <= ";
				// get date for this element
				if(isset($_POST[$this->name."Date"]))
					$this->value = $_POST[$this->name."Date"];
				else
				{
					$dtNow = new DateTime("now");
					$valueNow = $dtNow->format("Y-m-d H:i");
					$this->value = $valueNow ;
				}
				if ($this->enabled xor $this->invertEnabled)
					$condition = $this->field.$operator."'".$this->value."'";




				break;

			case "SingleEnum":

				break;

			case "MultiEnum":

				break;

			default:
				break;	

		}
//		echo $condition;
		return $condition;
	}


	public function getHTML()
	{
		$html = "";
		switch ($this->filterType)
		{
			case "Checkbox":
				$Checkbox = new Checkbox();
				$Checkbox->setName($this->name);
				$Checkbox->setText($this->text);
				$Checkbox->setChecked($this->enabled);
				$html .= "<tr><td>".$Checkbox->getHTML()."</td><td></td></tr>";


				break;

			case "Date":
				$Checkbox = new Checkbox();
				$Checkbox->setName($this->name);
				$Checkbox->setText($this->text);
				$Checkbox->setChecked($this->enabled);

				$input = new DateSelect();
				$input->setName($this->name."Date");
				$input->setDate($this->value);

				$html .= "<tr><td>".$Checkbox->getHTML()."</td><td>".$input->getHTML()."</td></tr>";

				break;

			case "SingleEnum":


				$Checkbox->setName($this->name);
				$Checkbox->setText($this->text);
				$Checkbox->setChecked($this->enabled);

/*
				$enumeration = new Enumeration($this->parentPage->getDatabase()->Recopy(),'EventTypes');
				$anArray = $enumeration->getArray();
				$input = new DropDownListEnumeration($anArray);
				if(isset($_POST['dtEventType']))
					$input->setSelectedValue($_POST['dtEventType']);
				$input->setName("dtEventType");
				$html .= "<tr><td>".$Checkbox->getHTML()."</td><td>".$input->getHTML()."</td></tr>";
*/

				break;

			case "MultiEnum":

				break;

			default:
				break;

		}
		return $html;
	}

}
